package softtech.apps.callblocker;



import java.util.ArrayList;

import softtech.apps.callblocker.model.BlackListItem;
import softtech.apps.callblocker.model.DatabaseHanler;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.database.Cursor;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.os.IBinder;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.Toast;

public class ServiceIcon extends Service{
	
	private WindowManager 				mWindowManager;			// Reference to the window
	private Button btWidget;//show wigdet
	private boolean mIsMoving;//drag and drog widget
	private String mPhoneNumber;//Phone number incoming call
	private String mName;//Name of phone number incoming call
	private boolean mIsBlacklist =true;//check black list
	Intent intent;
	
	@Override
	public IBinder onBind(Intent intent) {
		
		
		return null;
	}
	@Override
	public void onCreate() {
		
		super.onCreate();
		mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
//		Toast.makeText(getApplicationContext(), ""+mPhoneNumber, Toast.LENGTH_LONG).show();
		btWidget = new Button(this);
//		btWidget.setBackgroundResource(R.drawable.ic_launcher);

		    final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
		        WindowManager.LayoutParams.WRAP_CONTENT,
		        WindowManager.LayoutParams.WRAP_CONTENT,
		        WindowManager.LayoutParams.TYPE_PHONE,
		        WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
		        PixelFormat.TRANSLUCENT);

		    params.gravity = Gravity.TOP | Gravity.LEFT;
		    params.x = 0;
		    params.y = 100;

		    mWindowManager.addView(btWidget, params);
		    btWidget.setOnClickListener(new View.OnClickListener(
		    		) {
				
				@Override
				public void onClick(View v) {
					
					if(mIsMoving==false)
					Toast.makeText(getApplicationContext(), "click widget", Toast.LENGTH_SHORT).show();
					DatabaseHanler dbh = new DatabaseHanler(getApplicationContext());
					if(mIsBlacklist)
					{
				     Toast.makeText(getApplicationContext(), "Delete number black list", Toast.LENGTH_SHORT).show();
					 dbh.deleteBlackListItem(BlackListItem.PHONE_NUMBER + " =?", new String[]{mPhoneNumber});
					 btWidget.setBackgroundResource(R.drawable.widgetaddblack);
			    	 mIsBlacklist = false;
					}
					else
					{
					dbh.insertBlackListItem(new BlackListItem(mPhoneNumber, mName, 1));
						// 1 is mode default
					Toast.makeText(getApplicationContext(), "Is add to black list", Toast.LENGTH_SHORT).show();
					btWidget.setBackgroundResource(R.drawable.widgetdelete);
			    	mIsBlacklist = true;
					}
				}
			});
		    btWidget.setOnTouchListener(new View.OnTouchListener() {
		    	private int initialX;
		    	  private int initialY;
		    	  private float initialTouchX;
		    	  private float initialTouchY;
				@Override
				public boolean onTouch(View v, MotionEvent event) {
					
					switch (event.getAction()) {
				      case MotionEvent.ACTION_DOWN:
				        initialX = params.x;
				        initialY = params.y;
				        initialTouchX = event.getRawX();
				        initialTouchY = event.getRawY();
				        break;
				      case MotionEvent.ACTION_UP:
				    	 // v.performClick();
				    	  mIsMoving = false;
				    	  break;
				      case MotionEvent.ACTION_MOVE:
				        params.x = initialX + (int) (event.getRawX() - initialTouchX);
				        params.y = initialY + (int) (event.getRawY() - initialTouchY);
				        mWindowManager.updateViewLayout(btWidget, params);
				        mIsMoving = true;
				        break;
				    }
				    return false;
				}
			});
		
		    
		  
		    
		    
		    
		    
	}
	  @Override
	  public void onDestroy() {
		 
	      super.onDestroy();
	      //Toast.makeText(this, "destroyed", Toast.LENGTH_LONG).show();
	      if (btWidget != null)
			mWindowManager.removeView(btWidget);
	      
	  }
	  /**
	   * Get Button
	   * @return
	   */
	public Button getBtWidget() {
		return btWidget;
	}
	/**
	 * setButton
	 * @param btWidget
	 */
	public void setBtWidget(Button btWidget) {
		this.btWidget = btWidget;
	}
	/**
	 * get phone number
	 */
	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		
		this.intent = intent;
		  mPhoneNumber =(String) intent.getExtras().get("phonenumber");
		  mName = (String) intent.getExtras().get("name");
//			 Toast.makeText(getApplicationContext(), ""+mPhoneNumber, Toast.LENGTH_LONG).show();
			 DatabaseHanler dbh = new DatabaseHanler(getApplicationContext());
			 ArrayList<BlackListItem> alBlack = new ArrayList<BlackListItem>();
			 if(dbh.getBlackListItem(null, null, null, null, null)!=null){
				 alBlack = (ArrayList<BlackListItem>) dbh.getBlackListItem(null,
							BlackListItem.PHONE_NUMBER+ " =?", new String[] { mPhoneNumber },
							null, null);
				     if(alBlack.size()!=0){
				    	 btWidget.setBackgroundResource(R.drawable.widgetdelete);
				    	 mIsBlacklist = true;
				     }
				     else{
				    	 btWidget.setBackgroundResource(R.drawable.widgetaddblack);
				    	 mIsBlacklist = false;
				     }
			 }
		 return Service.START_STICKY;
	}
	

}
